package mspbots.qbd.online.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * mspbots.data.qbd.response.XmlResponseUtils
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2020/6/9
 */
public class XmlResponseUtils {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static ArrayNode visitChildNodes(Node nodeItem, String period, Long tenantId) {
        ArrayNode objectNodeList = OBJECT_MAPPER.createArrayNode();
        NodeList nList = nodeItem.getChildNodes();
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node node = nList.item(temp);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
                objectNode.put("timePeriod", period);
                objectNode.put("tenantId", tenantId);
                objectNode.put("type", node.getNodeName());
                //Check all attributes
                if (node.hasAttributes()) {
                    // get attributes names and values
                    NamedNodeMap nodeMap = node.getAttributes();
                    Node rowNumberNode = nodeMap.getNamedItem("rowNumber");
                    objectNode.put("rowNumber", rowNumberNode.getNodeValue());

                    ObjectNode valueNode = OBJECT_MAPPER.createObjectNode();
                    for (int i = 0; i < nodeMap.getLength(); i++) {
                        Node tempNode = nodeMap.item(i);
                        valueNode.put(tempNode.getNodeName(), tempNode.getNodeValue());
                    }
                    if (node.hasChildNodes()) {
                        //We got more childs; Let's visit them as well
                        ArrayNode objectNode1 = visitChildValNodes(node);
                        valueNode.set(node.getNodeName(), objectNode1);
                    }
                    objectNode.set("value", valueNode);
                }
                objectNodeList.add(objectNode);
            }
        }
        return objectNodeList;
    }

    public static ArrayNode visitChildValNodes(Node nodeItem) {
        NodeList nList = nodeItem.getChildNodes();
        ArrayNode objectNode = OBJECT_MAPPER.createArrayNode();
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node node = nList.item(temp);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                ObjectNode valueNode = OBJECT_MAPPER.createObjectNode();
                //Check all attributes
                if (node.hasAttributes()) {
                    // get attributes names and values
                    NamedNodeMap nodeMap = node.getAttributes();
                    for (int i = 0; i < nodeMap.getLength(); i++) {
                        Node tempNode = nodeMap.item(i);
                        valueNode.put(tempNode.getNodeName(), tempNode.getNodeValue());
                    }
                    if (node.hasChildNodes()) {
                        //We got more childs; Let's visit them as well
                        ArrayNode objectNode1 = visitChildValNodes(node);
                        valueNode.set(node.getNodeName(), objectNode1);
                    }
                }
                objectNode.add(valueNode);
            }
        }
        return objectNode;
    }
}
